package com.example.teamtribe.main.mine.sign;

import android.os.Bundle;
import android.support.annotation.Nullable;

import com.example.teamtribe.common.Back;
import com.example.teamtribe.utils.Utility;

public class SignPresenter implements SignContract.Presenter {

    private SignContract.View view;
    private SignModel model;

    public SignPresenter(SignContract.View view) {
        this.view = view;
        model = new SignModel();
    }

    @Override
    public void getSaying() {
        if (model.isToday()) {
            view.setSaying(model.getSaying());
            return;
        }

        model.getSaying(Utility.getToken(), new Back() {
            @Override
            public void success(@Nullable Bundle bundle) {
                view.setSaying(bundle.getString("saying"));
            }

            @Override
            public void failure(String meg) {
                view.showToast(meg);
            }
        });
    }

    @Override
    public void sign() {
        view.showProgress();
        model.sign(Utility.getToken(), new Back() {
            @Override
            public void success(@Nullable Bundle bundle) {
                view.hideProgress();
                view.setSign(true);
                getDays();
            }

            @Override
            public void failure(String meg) {
                view.hideProgress();
                view.setSign(false);
                view.showToast(meg);
            }
        });
    }

    @Override
    public void isSign() {
        if (model.isToday()) {
            view.setSign(model.isSign());
            return;
        }

        view.showProgress();
        model.isSign(Utility.getToken(), new Back() {
            @Override
            public void success(@Nullable Bundle bundle) {
                view.hideProgress();
                getDays();
                view.setSign(bundle.getBoolean("isSign"));
            }

            @Override
            public void failure(String meg) {
                view.hideProgress();
                view.setSign(false);
            }
        });
    }

    @Override
    public void getDays() {
        view.setDays(model.getDays());
        /*model.getDays(Utility.getToken(), new Back() {
            @Override
            public void success(@Nullable Bundle bundle) {
                view.setDays(bundle.getInt("getDays"));
            }

            @Override
            public void failure(String meg) {
                view.showToast(meg);
            }
        });*/
        if (model.getDays() < 25) {
            model.setDays(25);
        }
        view.setDays(model.getDays());
    }
}
